<?php
namespace App\Repository\Java;

use App\Repository\Contracts\GoodsCategoryInterface;
use App\Repository\Contracts\GoodsCategoryJavaInterface;
use Illuminate\Support\Facades\DB;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-02-06 13:39
 *
 * @Description:
 */
class GoodsCategoryRepository extends Repository implements GoodsCategoryJavaInterface
{
    protected function setPrefix()
    {
        $this->prefix = '';
    }

    public function syncList($createdAt = 0)
    {
        $page = 1;
        $rows = 10000;
        $form['updateDate'] = $createdAt;
        $result = $this->get(env('JAVA_RESOURCE').'goods/queryMerchMappingByConditionPaging.jhtml?page='.$page.'&rows='.$rows, $form);
        $insert = [];
        if ($result['total'] > 0) {
            foreach ($result['rows'] as $val) {
                $update = [
                    'category_id' => $val['id'], 'name' => $val['merchandiseName'], 'parent_id' => $val['parentId'],
                    'created_at' => strtotime($val['createDate']), 'updated_at' => strtotime($val['updateDate'])
                ];
                if (app(GoodsCategoryInterface::class)->detail($val['id'])) {
                    DB::table('goods_category')->where('category_id', $val['id'])->update($update);
                    continue;
                }
                $insert[] = $update;
            }
        }

        return  DB::table('goods_category')->insert($insert);
    }
}